Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” It is used to construct a complex object step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object.
Builder Pattern is used mostly when large number of difference representations of object are to be made but these objects use same common building processes (i.e., objects have same methods but those methods do a bit different stuff)
Builder does not create different kind of product it creates different representation of the product with common building process.
In this example there is a CIVILEngineer class that creates many different types of houses (objects) based on the object of the house blueprint classes. This example contains:
- House class: This class has methods for creating different parts of the house: setRoof(), setFloor(), setInterior(), setWalls(), etc
- house blueprint classes: Many other classes like IglooBlueprint class, TentHouseBlueprint class, BambooHutBlueprint class, etc creates an object of the House class and calls its methods with specific parameters to modify the object creation. Eg: IglooBlueprint class has a method
buildWalls()
which callshouse.setWalls('Ice blocks')
. - CIVILEngineer class: This class takes an object of one of the house blueprint classes and returns an object of House class
- Builder: This abstract base class defines all of the steps that must be taken in order to correctly create a product. Each step is generally abstract as the actual functionality of the builder is carried out in the concrete subclasses. The GetProduct method is used to return the final product. The builder class is often replaced with a simple interface. In the example above House class is a Builder class.
- ConcreteBuilder: There may be any number of concrete builder classes inheriting from Builder. These classes contain the functionality to create a particular complex product. In the example above IglooBlueprint class, TentHouseBlueprint class and BambooHutBlueprint class are ConcreteBuilder classes.
- Director: The director class controls the algorithm that generates the final product object. The director calls methods of the concrete builder in the correct order to generate the product object. On completion of the process, the GetProduct method of the builder object can be used to return the product. In the example above CIVILEngineer class is a Director class.
- Product: The product class defines the type of the complex object that is to be generated by the builder pattern. In the above example the user defines the type of the complex object that is to be generated therefor he / she acts like the Product
- The number of lines of code increase at least to double in builder pattern, but the effort pays off in terms of design flexibility and much more readable code.
- Requires creating a separate ConcreteBuilder for each different type of Product.
- Requires the builder classes to be mutable.
- It provides clear separation between the construction and representation of an object.
- It provides better control over construction process.
- It supports to change the internal representation of objects.
- Builder design pattern also helps in minimizing the number of parameters in constructor and thus there is no need to pass in null for optional parameters to the constructor.
- Object is always instantiated in a complete state.